<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $count = M('question')->where("tip='0'")->count();

        $page = new \Think\Page($count,3);

        $show = $page->show();

        $this->assign('show',$show);

    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip='0' limit ".$page->firstRow.",".$page->listRows;
    	$arr = M()->query($sql);
    	$this->assign('arr1',$arr);

        

    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip<>'0' order by t1.create_time desc limit 5";

    	$ars = M()->query($sql);

    	$this->assign('ars',$ars);
        $this->assign('title','首页');
    	// exit;
    	$this->getTopuser();
    	$this->getTopview();
    	$this->getTopanswer();
       $this->display();
    }
    public function wu()
    {
        $sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status='未结贴' order by t1.create_time desc";
        $sql = M()->query($sql);
        $this->assign('title','首页');
        $this->getTopuser();
        $this->getTopview();
        $this->getTopanswer();
        // var_dump($sql);
        // exit;
        $this->assign('sql',$sql);
        $this->display();
    }
    public function cao()
    {
        $sql = "select * from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status='已采纳' order by t1.create_time desc";
        $sql = M()->query($sql);
        $this->assign('sql',$sql);
        $this->assign('title','首页');
        $this->getTopuser();
        $this->getTopview();
        $this->getTopanswer();
        $this->display();
    }
    public function jing()
    {
        $sql = "select * from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip='2' order by t1.create_time desc";
        $sql = M()->query($sql);
        $this->assign('sql',$sql);
        $this->assign('title','首页');
        $this->getTopuser();
        $this->getTopview();
        $this->getTopanswer();
       $this->display();
    }
    public function my()
    {
        $id = $_SESSION['id'];
        
        $sql = "select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.uid='{$id}' order by t1.create_time desc";
        $sql = M()->query($sql);
        // $sql = current($sql);
        $this->assign('sql',$sql);
        $this->assign('title','首页');
        $this->getTopuser();
        $this->getTopview();
        $this->getTopanswer();
       $this->display();
    }
    public function so()
    {
       $q = I('get.q');
        Vendor('Pscws4.pscws4','','.class.php');
        $pscws = new \PSCWS4('utf8');

        $pscws->set_dict('./Public/dict.utf8.xdb');
        $pscws->send_text($q);

        $tops = $pscws->get_tops(5, 'n,v');

        $sql = "select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id where ";

        if ($tops) {
            foreach ($tops as $v) {
           $sql .="title like '%".$v['word']."%'or ";
            }

            $sql = rtrim($sql,'or ');
        }else{
            $sql .="title like '%".$q."%'";
            
        }
        

        $res = M()->query($sql);


        // $res = current($res);
        foreach ($res as $k => $v) {
            foreach ($tops as  $a) {
                $res[$k]['title']=str_replace($a['word'],'<span style="color:red">'.$a['word'].'</span>', $v['title']);
            }
        }
        

        $this->assign('res',$res);
        // var_dump($res);
        $this->display();
    }
}